## How do people evaluate foreign aid to “nasty” regimes?
## Tobias Heinrich & Yoshiharu Kobayashi
## British Journal of Political Science
#########################################################


## Graphs for incremental change analysis

## Load outputs
load("output/ExpModels.Rdata")


## Main graphs for A2-A4
########################
out <- c()
for(i in 2:4) for(j in 1:3) if(i != 2) out <- rbind(out, all_exp_mod[[i]][[j]]$BestResponseMR)
out$Which <- as.character(out$Which)
out$Which[out$Case == "Placebo"] <- "Placebo"
out$Which <- factor(out$Which, level=c("Placebo", "25", "50", "75", "Baseline", paste0("Benefits", 1:4)))
out$Scenario <- as.character(out$Which)

## Prettify Scenario
out$Scenario[out$Which == "Benefits1"] <- "Small CT"
out$Scenario[out$Which == "Benefits2"] <- "Large CT"
out$Scenario[out$Which == "Benefits3"] <- "Small AML"
out$Scenario[out$Which == "Benefits4"] <- "Large AML"
out$Scenario[out$Which == "25"] <- "$25m"
out$Scenario[out$Which == "50"] <- "$50m"
out$Scenario[out$Which == "75"] <- "$75m"
out$Scenario <- factor(out$Scenario, levels=c("Placebo", "Baseline", "$25m", "$50m", "$75m",
                                              "Small AML", "Large AML", "Small CT", "Large CT"))

ggtitles <- c("Diverting strategy", "Distancing strategy")
a_list <- c("A3", "A4")

for(j in 1:3)
{
  g_list <- vector("list", 2)
  for(i in 1:2)
  {
    g <- ggplot(data=subset(out, w == j & Info == a_list[i]), aes(x=What, y=q_m, ymin=q_l, ymax=q_u, colour=Scenario, group=Scenario))
    g <- g + geom_linerange(size=1, position=position_dodge(width=.3))
    g <- g + geom_point(size=2.8, position=position_dodge(width=.3))
    g <- g + scale_colour_grey() + xlab("")
    g <- g + geom_hline(yintercept=0, size=.8)
    g <- g + ylab("Conditional effect") + ggtitle(ggtitles[i])
    g <- g + theme_bw() + scale_y_continuous(limits=c(min(out$q_l), max(out$q_u)))
    g <- g + theme(strip.text.x=element_text(hjust=1, vjust=.2, size=rel(1.0)),
                   strip.text.y=element_text(hjust=0, size=rel(1.0)),
                   plot.title=element_text(size=rel(1.3), hjust=0),
                   axis.text=element_text(size=rel(.81)))
    g <- g + geom_linerange(data=subset(out, w == j & Info == a_list[i] & Scenario == "Placebo"), size=1.2, colour="black")
    g <- g + geom_point(data=subset(out, w == j & Info == a_list[i] & Scenario == "Placebo"), size=3, colour="black")
    g_list[[i]] <- g
  }
  
  pdf(file=paste0("output/figures/IC-PI-B-C-w", j, ".pdf"), width=11, height=8)
  grid.draw(rbind(ggplotGrob(g_list[[2]]),
                  ggplotGrob(g_list[[1]]), size="last"))
  dev.off()
}


## Graph for A1 analysis
########################
out <- c()
for(j in 1:3) out <- rbind(out, all_exp_mod[[1]][[j]]$BestResponseMR)
out$Which <- as.character(out$Which)
out$Which[out$Case == "Placebo"] <- "Placebo"
out$Which <- factor(out$Which, level=c("Placebo", "No remedial aid", "Best response"))
out$Scenario <- out$Which
for(j in 1:3)
{
  g <- ggplot(data=subset(out, w == j), aes(x=What, y=q_m, ymin=q_l, ymax=q_u, colour=Scenario, group=Scenario))
  g <- g + geom_linerange(size=1, position=position_dodge(width=.3))
  g <- g + geom_point(size=2.8, position=position_dodge(width=.3))
  g <- g + scale_colour_grey() + xlab("")
  g <- g + geom_hline(yintercept=0, size=.8)
  g <- g + ylab("Conditional change") + ggtitle("Addressing strategy")
  g <- g + theme_bw() + scale_y_continuous(limits=c(-5.3, 0))
  g <- g + theme(strip.text.x=element_text(hjust=1, vjust=.2, size=rel(1.0)),
                 strip.text.y=element_text(hjust=0, size=rel(1.0)),
                 plot.title=element_text(size=rel(1.3), hjust=0),
                 axis.text=element_text(size=rel(.81)))
  
  ggsave(file=paste0("output/figures/IC-Remedy-w", j, ".pdf"),
         width=12, height=4)
}




